package com.pig4cloud.mall.order.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.mall.order.entity.OrderItem;
import com.pig4cloud.mall.order.service.OrderItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 订单中所包含的商品
 *
 * @author fangyu
 * @date 2020-12-25 17:48:25
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/orderitem")
@Api(value = "orderitem" , tags = "订单中所包含的商品管理")
public class OrderItemController {

    private final  OrderItemService orderItemService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param orderItem 订单中所包含的商品
     * @return
     */
    @ApiOperation(value = "分页查询" , notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('order_orderitem_get')")
    public R getOrderItemPage(Page page, OrderItem orderItem) {
        return R.ok(orderItemService.page(page, Wrappers.query(orderItem)));
    }


    /**
     * 通过id查询订单中所包含的商品
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询" , notes = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('order_orderitem_get')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(orderItemService.getById(id));
    }

    /**
     * 新增订单中所包含的商品
     * @param orderItem 订单中所包含的商品
     * @return R
     */
    @ApiOperation(value = "新增订单中所包含的商品" , notes = "新增订单中所包含的商品")
    @SysLog("新增订单中所包含的商品")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('order_orderitem_add')")
    public R save(@RequestBody OrderItem orderItem) {
        return R.ok(orderItemService.save(orderItem));
    }

    /**
     * 修改订单中所包含的商品
     * @param orderItem 订单中所包含的商品
     * @return R
     */
    @ApiOperation(value = "修改订单中所包含的商品" , notes = "修改订单中所包含的商品")
    @SysLog("修改订单中所包含的商品")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('order_orderitem_edit')")
    public R updateById(@RequestBody OrderItem orderItem) {
        return R.ok(orderItemService.updateById(orderItem));
    }

    /**
     * 通过id删除订单中所包含的商品
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除订单中所包含的商品" , notes = "通过id删除订单中所包含的商品")
    @SysLog("通过id删除订单中所包含的商品")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('order_orderitem_del')")
    public R removeById(@PathVariable Long id) {
        return R.ok(orderItemService.removeById(id));
    }

}
